import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class IconDemo extends StatelessWidget {
  const IconDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(

      padding: const EdgeInsets.symmetric(horizontal: 8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          ActionButton(
            title: '相机',
            icon: Icons.camera_alt,
          ),
          ActionButton(
            title: '手写',
            icon: CupertinoIcons.pen,
          ),
          ActionButton(
            title: '对话',
            image: AssetImage('images/ic_double_mic_24dp.png'),
          ),
          ActionButton(
            title: '语音',
            icon: Icons.keyboard_voice,
          ),
        ],
      ),
    );
  }
}

class ActionButton extends StatefulWidget {
  final String title;
  final IconData? icon;
  final AssetImage? image;

  const ActionButton({Key? key, required this.title, this.icon, this.image})
      : super(key: key);

  @override
  _ActionButtonState createState() => _ActionButtonState();
}

class _ActionButtonState extends State<ActionButton> {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding:
          const EdgeInsets.only(left: 10.0, right: 10.0, top: 2.0, bottom: 2.0),
      child: TextButton(
        onPressed: () {},
        child: Column(
          children: [
            _displayIcon(),
            SizedBox(height: 10,),
            Text(
              widget.title,
              style: TextStyle(color: Colors.black, fontSize: 15.0),
            )
          ],
        ),
      ),
    );
  }

  _displayIcon() {
    if (widget.icon != null) {
      return Icon(
        widget.icon,
        color: Color(0xff3f51b5),
        size: 25.0,
      );
    } else if (widget.image != null) {
      return ImageIcon(
        widget.image,
        color: Color(0xff3f51b5),
        size: 25.0,
      );
    }

    return Container();
  }
}
